//
//  VOCMemoryViewController.m
//  iVoc
//
//  Created by Wallace Wu on 09-4-9.
//  Copyright 2009 Sonic Solutions. All rights reserved.
//

#import "VOCMemoryViewController.h"
#import "VOCWordData.h"
#import "VOCResultViewController.h"

@implementation VOCMemoryViewController

@synthesize wordList;
@synthesize rememberWord;

// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
        beginNextWord = YES;
        self.hidesBottomBarWhenPushed = YES;
        currentWordIndex = 0;
    }
    return self;
}

-(void)nextWord
{
    VOCWordData *currentWord = nil;
    currentWord = [wordList.data objectAtIndex:currentWordIndex];
    labelWord.text = [currentWord word];
    labelFunction.text = [NSString stringRepOfFuntion:((VOCWordExp*)[[currentWord explanations] objectAtIndex:0]).function];
    labelExplanation.text = ((VOCWordExp*)[currentWord.explanations objectAtIndex:0]).explanation;

    labelWord.hidden = rememberWord;
    labelExplanation.hidden = !rememberWord;
    
    labelFunction.hidden = YES;
    labelHints.hidden = NO;
    
    btnRight.enabled = NO;
    btnWrong.enabled = NO;
    
    currentWordIndex ++;
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    labelHints.font = [UIFont fontWithName:@"Times New Roman" size:20];
    labelHints.text = NSLocalizedString(@"TAP_HINT",nil); 
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"MEM_FINISH",nil) style:UIBarButtonItemStyleBordered target:self action:@selector(finish)] autorelease];
    [self nextWord];        
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    [self.navigationController popViewControllerAnimated:YES]; 
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSUInteger count = [[touches anyObject] tapCount];
    
    //double tap
    if(count == 2)
    {
        // show answer
        labelWord.hidden = NO;
        labelFunction.hidden = NO;
        labelExplanation.hidden = NO;
        labelHints.hidden = YES;
        
        btnRight.enabled = YES;
        btnWrong.enabled = YES;
    }
}

-(BOOL)hasEnd
{
    return currentWordIndex == [wordList.data count];
}

-(void)finish
{
    VOCResultViewController *resultViewController = [[VOCResultViewController alloc] initWithNibName:@"VOCResultViewController" bundle:nil];
    resultViewController.rightTimes = rightTimes;
    resultViewController.wrongTimes = wrongTimes;
    [self.navigationController pushViewController:resultViewController animated:YES];
    [resultViewController release];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

-(IBAction)next:(id)sender
{   
    if(sender == btnRight)
    {
        rightTimes ++;
    }
    if(sender == btnWrong)
    {
        wrongTimes ++;
    }
    if([self hasEnd] )
    {
        [self finish];
        return;
    }
    
    [self nextWord];
}


- (void)dealloc {
    [labelWord release];
    [labelFunction release];
    [labelExplanation release];
    [btnRight release];
    [btnWrong release];
    [wordList release];
    [super dealloc];
}


@end
